package com.caigou.modules.rank.ui.level

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.fragment.app.Fragment
import com.caigou.comm.base.container.BaseFragment
import com.caigou.comm.base.widget.DatePickDialog
import com.caigou.modules.rank.R
import com.caigou.modules.rank.adapter.RankPagerAdapter
import com.caigou.modules.rank.databinding.FragmentLevelRankBinding
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import dagger.hilt.android.AndroidEntryPoint
import java.util.*

/**
 * @author #Suyghur.
 * Created on 2021/11/03
 */
@AndroidEntryPoint
class LevelRankFragment : BaseFragment() {

    private var _binding: FragmentLevelRankBinding? = null
    private val binding get() = _binding!!

    private var monthLevelRankFragment: MonthLevelRankFragment? = null
    private var totalLevelRankFragment: TotalLevelRankFragment? = null

    private var month: Int = 0

    private val fragments = mutableListOf<Fragment>()

    private val pagerAdapter: RankPagerAdapter by lazy {
        RankPagerAdapter(this)
    }

    private val calendarDialog by lazy {
        DatePickDialog(requireActivity())
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentLevelRankBinding.inflate(inflater, container, false)
        return _binding?.root
    }

    override fun initView() {
        month = Calendar.getInstance().get(Calendar.MONTH) + 1
        monthLevelRankFragment = MonthLevelRankFragment()
        totalLevelRankFragment = TotalLevelRankFragment()

        binding.levelRankViewPager.adapter = pagerAdapter
        binding.levelRankViewPager.isUserInputEnabled = false
        TabLayoutMediator(binding.levelRankTabLayout, binding.levelRankViewPager, true, true) { tab, position ->
            tab.text = if (position == 0) {
                "${month}月等级排行榜"
            } else {
                "等级排行总榜"
            }
        }.attach()

        setTabBackground(R.drawable.rank_level_tab_left_selected, R.drawable.rank_level_tab_right_select)
        binding.levelRankTabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                when (binding.levelRankTabLayout.selectedTabPosition) {
                    0 -> setTabBackground(R.drawable.rank_level_tab_left_selected, R.drawable.rank_level_tab_right_select)
                    1 -> setTabBackground(R.drawable.rank_level_tab_left_select, R.drawable.rank_level_tab_right_selected)
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }
        })

        fragments.add(monthLevelRankFragment!!)
        fragments.add(totalLevelRankFragment!!)

        pagerAdapter.setList(fragments)
        binding.levelRankViewPager.offscreenPageLimit = 2

        binding.levelRankIvCalendar.setOnClickListener {
            calendarDialog.show()
        }
        calendarDialog.tvConfirm.setOnClickListener {
            calendarDialog.dismiss()
            monthLevelRankFragment!!.refreshData(calendarDialog.getDate())
        }
        calendarDialog.tvCancel.setOnClickListener {
            calendarDialog.dismiss()
        }

        binding.levelRankTvBack.setOnClickListener {
            requireActivity().finish()
        }
    }

    override fun lazyLoad() {
    }

    override fun onBack() {
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    private fun setTabBackground(left: Int, right: Int) {
        val tabStrip = binding.levelRankTabLayout.getChildAt(0) as ViewGroup
        val tabView1 = tabStrip.getChildAt(0)
        val tabView2 = tabStrip.getChildAt(1)
        if (tabView1 != null) {
            val paddingStart = tabView1.paddingStart
            val paddingTop = tabView1.paddingTop
            val paddingEnd = tabView1.paddingEnd
            val paddingBottom = tabView1.paddingBottom
            ViewCompat.setBackground(
                tabView1, ContextCompat.getDrawable(tabView1.context, left)
            )
            ViewCompat.setPaddingRelative(tabView1, paddingStart, paddingTop, paddingEnd, paddingBottom)
        }
        if (tabView2 != null) {
            val paddingStart = tabView2.paddingStart
            val paddingTop = tabView2.paddingTop
            val paddingEnd = tabView2.paddingEnd
            val paddingBottom = tabView2.paddingBottom
            ViewCompat.setBackground(tabView2, ContextCompat.getDrawable(tabView2.context, right))
            ViewCompat.setPaddingRelative(tabView2, paddingStart, paddingTop, paddingEnd, paddingBottom)
        }
    }
}